package com.example.cnotebook.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.cnotebook.R;
import com.example.cnotebook.pojo.AudioInfo;

import java.io.IOException;
import java.util.List;

public class AudioListViewAdapter extends BaseAdapter {
    private List<AudioInfo> audioInfos;
    private Context context;
    private CallBack callBack;
    private int prePosition = -1;

    public AudioListViewAdapter(List<AudioInfo> audioInfos, Context context) {
        this.audioInfos = audioInfos;
        this.context = context;
    }

    public AudioListViewAdapter(List<AudioInfo> audioInfos, Context context, CallBack callBack) {
        this.audioInfos = audioInfos;
        this.context = context;
        this.callBack = callBack;
    }

    @Override
    public int getCount() {
        return audioInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return audioInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_audio_layout, null);
            holder = new ViewHolder();
            holder.tvDate = convertView.findViewById(R.id.tv_date);
            holder.ivPlayAudio = convertView.findViewById(R.id.iv_play_audio);
            holder.ivPauseAudio = convertView.findViewById(R.id.iv_pause_audio);
//            holder.progressAudio = convertView.findViewById(R.id.progress_audio);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final AudioInfo audioInfo = audioInfos.get(position);
        holder.tvDate.setText(audioInfo.getCreateDate());
        if (audioInfo.isPlay()) {
            holder.ivPlayAudio.setVisibility(View.GONE);
            holder.ivPauseAudio.setVisibility(View.VISIBLE);
        } else {
            holder.ivPlayAudio.setVisibility(View.VISIBLE);
            holder.ivPauseAudio.setVisibility(View.GONE);
        }
        audioInfo.getMediaPlayer().setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                System.out.println("-----------------------------");
                audioInfo.setPlay(false);
                AudioListViewAdapter.this.notifyDataSetChanged();
            }
        });
        holder.ivPlayAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (prePosition != -1) {
                    AudioInfo info = audioInfos.get(prePosition);
                    info.setPlay(false);
                    info.getMediaPlayer().stop();
                }
                try {
//                    stopAudio(audioInfo);
                    audioInfo.setPlay(true);
                    playAudio(audioInfo);
                    prePosition = position;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                AudioListViewAdapter.this.notifyDataSetChanged();
            }
        });
        holder.ivPauseAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    stopAudio(audioInfo);
                    prePosition = -1;
                    audioInfo.setPlay(false);
                    AudioListViewAdapter.this.notifyDataSetChanged();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        return convertView;
    }

    private void playAudio(AudioInfo audioInfo) throws IOException {
        MediaPlayer mediaPlayer = audioInfo.getMediaPlayer();
        mediaPlayer.prepare();
        mediaPlayer.start();
    }

    private void stopAudio(AudioInfo audioInfo) throws IOException {
        MediaPlayer mediaPlayer = audioInfo.getMediaPlayer();
        mediaPlayer.stop();
//        mediaPlayer.release();
    }

    static class ViewHolder {
        TextView tvDate;
        ImageView ivPlayAudio;
        ImageView ivPauseAudio;
        ProgressBar progressAudio;
    }

    interface CallBack {
        void playAudioCallBack(AudioInfo audioInfo);
    }
}
